package com.zealfi.wk.service.controller;

import com.zealfi.wk.api.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.*;

/**
 * Created by kuan at 2018-03-30 下午 2:36
 */
@RestController
@RequestMapping("hello")
public class HelloController {
    private static final Logger log = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private DiscoveryClient client;
    @Autowired
    private Registration registration;

    @RequestMapping(value = "1", method = RequestMethod.GET)
    public String hello(String name) {
        return "hello " + name;
    }

    @RequestMapping(value = "2", method = RequestMethod.GET)
    public User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age) {
        return new User(name, age);
    }

    @RequestMapping(value = "2", method = RequestMethod.POST)
    public String hello(@RequestBody User user) {
        return "hello " + user.getName() + "," + user.getAge();
    }

    @RequestMapping(method = RequestMethod.GET)
    public String get() {
        return pong("/hello.get, host:");
    }

    @RequestMapping(method = RequestMethod.POST)
    public String post() {
        return pong("/hello.post, host:");
    }

    @RequestMapping(method = RequestMethod.PUT)
    public String put() {
        return pong("/hello.put, host:");
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public String delete() {
        return pong("/hello.delete, host:");
    }

    private String pong(String s) {
        ServiceInstance instance = client.getLocalServiceInstance();

        log.info(s + instance.getHost() + " ,port:" + instance.getPort() + " ,service_id:" + instance.getServiceId());
        return "Hello World";
    }
}
